{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from PIL import Image\n",
    "import scipy.io as sio\n",
    "import numpy as np\n",
    "\n",
    "mat_data = sio.loadmat('/home/vivien/code/data/ActiveVisionDataset_part1/ActiveVisionDataset/Home_001_1/image_structs.mat')\n",
    "\n",
    "image_structs = mat_data['image_structs']\n",
    "scale = mat_data['scale'].squeeze()\n",
    "print(scale)\n",
    "struct1 = image_structs[0,1]\n",
    "\n",
    "K = np.array(struct1[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "base_root = '/home/vivien/code/data/ActiveVisionDataset_part1/ActiveVisionDataset/'\n",
    "dataset_name = 'Home_001_1'\n",
    "rgb_prefix = 'jpg_rgb'\n",
    "depth_prefix = 'high_res_depth'\n",
    "jpg_sample_name = '000110{:0>5}0101.jpg'\n",
    "depth_sample_name = '000110{:0>5}0103.png'\n",
    "rgb_file_name = os.path.join(base_root,dataset_name,rgb_prefix,jpg_sample_name.format(112))\n",
    "depth_file_name = os.path.join(base_root,dataset_name,depth_prefix,depth_sample_name.format(112))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "depth = Image.open(depth_file_name)\n",
    "depth_array = np.array(depth)\n",
    "array = [[(i,j) for i in range(depth_array.shape[1])] for j in range(depth_array.shape[0])]\n",
    "loc_array = np.array(array)\n",
    "depth_expand = np.expand_dims(depth_array,2)\n",
    "data = np.concatenate((loc_array,depth_expand),axis=2)\n",
    "data[:,:,2] = data[:,:,2] / scale\n",
    "data = np.expand_dims(data,3)\n",
    "projected_p = np.matmul(K,data)\n",
    "projected_p = projected_p / scale\n",
    "S = 9\n",
    "projected_p_onedim = projected_p.reshape((-1,3,1))\n",
    "projected_p_onedim = projected_p_onedim.squeeze(2)\n",
    "projected_p_onedim[:,0] = projected_p_onedim[:,0]*(S-1)/2 + (S+1)/2\n",
    "projected_p_onedim[:,2] = projected_p_onedim[:,2]*(S-1)/2 + (S+1)/2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.0\n",
      "17.09133275603498\n",
      "5.0\n",
      "12.397772427802744\n"
     ]
    }
   ],
   "source": [
    "print(np.min(projected_p_onedim[:,0]))\n",
    "print(np.max(projected_p_onedim[:,0]))\n",
    "print(np.min(projected_p_onedim[:,2]))\n",
    "print(np.max(projected_p_onedim[:,2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3.12930891e-03, -9.10466551e-05,  1.86292452e-03])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "projected_p_onedim.squeeze(2)[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.sparse import csr_matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'np' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-753b406f5ff8>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mrow\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0mcol\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mcsr_matrix\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrow\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mcol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtoarray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mNameError\u001b[0m: name 'np' is not defined"
     ]
    }
   ],
   "source": [
    "data = [0.1,0.6,0.3]\n",
    "row = [1,0,0]\n",
    "col = [0,1,1]\n",
    "csr_matrix((data,(row,col)),dtype=np.float).toarray()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[433.67959415],\n",
       "       [487.18723846],\n",
       "       [273.08262293]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "image_coord = np.array([500,500,1.9]).reshape((3,1))\n",
    "np.matmul(K,image_coord)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
